/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.perficient.ibuzz.ui;

import com.perficient.ibuzz.controller.IBuzzController;
import java.awt.Image;
import java.awt.Panel;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.plaf.metal.MetalIconFactory.FileIcon16;

/**
 *
 * @author Trainning
 */
public class BuzzWindowAdapter extends WindowAdapter {

    private IBuzzController controller;

    public BuzzWindowAdapter(IBuzzController contr) {
        this.controller = contr;
    }

    @Override
    public void windowIconified(WindowEvent e) {
        Icon defaultIcon = new FileIcon16();

        Image dummy = new BufferedImage(16, 16, BufferedImage.TYPE_4BYTE_ABGR);
        defaultIcon.paintIcon(new Panel(), dummy.getGraphics(), 0, 0);

        SystemTray tray = SystemTray.getSystemTray();
        BuzzTrayIcon icon = null;
        try {
            icon = new BuzzTrayIcon(dummy, controller);
            new Thread(icon).start();

            icon.addMouseListener(new TrayIconMouseListener(e.getWindow(), icon));
            tray.add(icon);
        } catch (Exception ex) {
            Logger.getLogger(FormMessageSender.class.getName()).log(Level.SEVERE, null, ex);
        }
        e.getWindow().setVisible(false);

    }
}
